package com.xxxx.yebserver.pojo;

import com.xxxx.yebserver.entity.Admin;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MySecurityUser implements UserDetails, Serializable {
    private static final long serialVersionUID = 1L;
    @Getter @Setter
    private transient Admin admin;

    @Getter @Setter
    private List<String> premissionList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> list = new ArrayList<>();
        if(premissionList != null && premissionList.size()>0){
            for (String premission : premissionList) {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(premission);
                list.add(authority);
            }
        }
        return list;
    }

    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return admin.getStatus() == 1;
    }
}
